package com.xqboss.apps.controller.punch;

import com.xqboss.apps.domain.user.UserIdentityRankSlot;
import com.xqboss.apps.dto.punch.EditUserPunchConfigDto;
import com.xqboss.apps.dto.punch.UserAddSlotDto;
import com.xqboss.apps.dto.punch.UserPunchSlotDto;
import com.xqboss.apps.service.punch.PunchUserConfigService;
import com.xqboss.apps.service.user.UserIdentityRankSlotService;
import com.xqboss.apps.vo.punch.EstimateIncomeVo;
import com.xqboss.apps.vo.punch.UserPunchConfigVo;
import com.xqboss.apps.vo.punch.UserPunchIndexVo;
import com.xqboss.common.annotation.RepeatSubmit;
import com.xqboss.common.core.controller.BaseController;
import com.xqboss.common.core.domain.R;
import com.xqboss.common.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author Trent
 * @date 2023/8/15
 */
@Api(tags = "打卡-用户打卡配置")
@RestController
@RequestMapping("/punch/config")
public class PunchUserConfigController extends BaseController {

    @Autowired
    private PunchUserConfigService punchUserConfigService;
    @Autowired
    private UserIdentityRankSlotService userIdentityRankSlotService;


    @ApiOperation("获取用户打卡配置")
    @GetMapping("get")
    public R<UserPunchConfigVo> getUserConfig(){
        UserPunchConfigVo vo = punchUserConfigService.getPunchConfigVo(getUserId());
        return R.ok(vo);
    }

    @RepeatSubmit(interval = 500, justUrl = true)
    @ApiOperation("修改用户打卡配置")
    @PutMapping("edit")
    public R editUserConfig(@Validated @RequestBody EditUserPunchConfigDto params){
        if(Objects.isNull(params.getUserNftId())){
            params.setPunchRuleId(null);
        }else{
            if(Objects.isNull(params.getPunchRuleId())){
                throw new ServiceException("请选择打卡时段！");
            }
        }
        punchUserConfigService.updateConfig(getUserId(), params.getPunchRuleId(), params.getUserNftId());
        return R.ok();
    }

    @ApiOperation("预估打卡收益")
    @GetMapping("estimate-income")
    public R<EstimateIncomeVo> estimatePunchIncome(@Validated EditUserPunchConfigDto params){
        BigDecimal bigDecimal = punchUserConfigService.estimatePunchIncome(getUserId(), params.getPunchRuleId(), params.getUserNftId());
        EstimateIncomeVo vo = new EstimateIncomeVo();
        vo.setIncome(bigDecimal);
        return R.ok(vo);
    }


    @ApiOperation("多卡槽-信息")
    @GetMapping("slot")
    public R<UserPunchIndexVo> getPunchIndex(){
        UserPunchIndexVo vo = punchUserConfigService.getPunchIndex(getUserId());
        return R.ok(vo);
    }

    @RepeatSubmit(interval = 500, justUrl = true)
    @ApiOperation("卡槽-快捷添加装备")
    @PutMapping("slot/add")
    public R slotAdd(@Validated @RequestBody UserAddSlotDto params) {
        punchUserConfigService.slotAdd(getUserId(), params);
        return R.ok();
    }

    @RepeatSubmit(interval = 500, justUrl = true)
    @ApiOperation("卡槽-卸下装备")
    @PutMapping("slot/wholesale")
    public R slotWholesale(@Validated @RequestBody UserAddSlotDto params) {
        punchUserConfigService.slotWholesale(getUserId(), params);
        return R.ok();
    }

    @RepeatSubmit(interval = 800, justUrl = true)
    @ApiOperation("卡槽-切换装备")
    @PutMapping("slot/handoff")
    public R slotHandoff(@Validated @RequestBody EditUserPunchConfigDto params) {
        punchUserConfigService.slotHandoff(getUserId(), params);
        return R.ok();
    }

    @ApiOperation("卡槽-预解锁")
    @GetMapping("slot/pre-unlock/{id}")
    public R<UserIdentityRankSlot> preUnlock(@PathVariable("id")
                                             @ApiParam(value = "卡槽id", required = true)
                                             @NotNull(message = "请输入卡槽id")
                                             Long id) {
        UserIdentityRankSlot vo = userIdentityRankSlotService.getById(id);
        return R.ok(vo);
    }

    @RepeatSubmit(interval = 1000, justUrl = true)
    @ApiOperation("卡槽-解锁/加时")
    @PostMapping("slot/unlock")
    public R<Boolean> unlock(@RequestBody @Validated UserPunchSlotDto dto) {
        return R.ok(userIdentityRankSlotService.unlock(getUserId(), dto));
    }

}
